package com.itqf.dao.impl;

import com.itqf.dao.UserDao;
import com.itqf.pojo.User;
import com.itqf.utils.C3P0Utils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;

/**
 * projectName: gpshop
 *
 * @author: 赵伟风
 * time: 2021/7/20 15:18
 * description:
 */
public class UserDaoImpl implements UserDao {


    /**
     * 根据账号查询数据库存在的数量
     *
     * @param uAccount
     * @return
     */
    @Override
    public long queryAccount(String uAccount) throws SQLException {

        String sql = "select count(1) from user where u_account = ?;";

        QueryRunner queryRunner  = new QueryRunner(C3P0Utils.getDataSource());

        Long count = (Long) queryRunner.query(sql, new ScalarHandler(), uAccount);

        return count;
    }


    /**
     * 插入用户数据到数据库
     *
     * @param user 要插入的数据
     * @return 返回插入影响的行数
     */
    @Override
    public int insertUser(User user) throws SQLException {

        String sql = "insert into user (u_account,u_password,u_sex,u_email,u_state,u_token,u_role) " +
                "value(?,?,?,?,?,?,?);";

        QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource());

        int rows = queryRunner.update(sql, user.getuAccount(),user.getuPassword(),user.getuSex(),
                user.getuEmail(),user.getuState(),user.getuToken(),user.getuRole());

        return rows;
    }

    @Override
    public Long queryToken(String token) throws SQLException {

        String sql = "select count(1) from user where u_token = ?;";

        QueryRunner queryRunner  = new QueryRunner(C3P0Utils.getDataSource());

        Long count = (Long) queryRunner.query(sql, new ScalarHandler(), token);

        return count;
    }

    @Override
    public int updateUser(String token) throws SQLException {

        String sql = "update user set u_state = ? , u_token = ? where u_token = ?;";

        QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource());

        int rows = queryRunner.update(sql,1,"",token);


        return rows;
    }

    /**
     * 用户登录查询
     *
     * @param uAccount
     * @param newPassword
     * @return
     */
    @Override
    public User queryUser(String uAccount, String newPassword) throws SQLException {

        String sql = "select u_id as uId, u_account as uAccount,u_password as uPassword,u_sex as uSex,u_email as uEmail,u_state as uState,u_token as uToken,u_role as uRole,token from user where u_account = ? and u_password = ? ;";

        QueryRunner queryRunner  = new QueryRunner(C3P0Utils.getDataSource());

        User user = queryRunner.query(sql, new BeanHandler<>(User.class),uAccount,newPassword);

        return user;
    }

    /**
     * 根据用户id修改token!
     *
     * @param uid
     * @param token
     */
    @Override
    public void updateTokenById(int uid, String token) throws SQLException {

         String sql = "update user set  token = ? where u_id = ?;";

        QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource());

        int rows = queryRunner.update(sql,token,uid);

    }

    @Override
    public User queryUserByToken(String token) throws SQLException {

        String sql = "select u_id as uId, u_account as uAccount,u_password as uPassword,u_sex as uSex,u_email as uEmail,u_state as uState,u_token as uToken,u_role as uRole,token from user where token = ? ;";

        QueryRunner queryRunner  = new QueryRunner(C3P0Utils.getDataSource());

        User user = queryRunner.query(sql, new BeanHandler<>(User.class),token);

        return user;
    }
}
